package my.edu.model.commodity.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import my.edu.common.constant.MessageConstant;
import my.edu.common.exception.CanNotFoundException;
import my.edu.common.exception.PriceSchemeException;
import my.edu.model.commodity.entity.PriceField;
import my.edu.model.commodity.entity.PricingSchemeDetail;
import my.edu.model.commodity.entity.dto.PricingSchemeDTO;
import my.edu.model.commodity.entity.vo.PricingSchemeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import my.edu.model.commodity.entity.PricingScheme;
import my.edu.model.commodity.mapper.PricingSchemeMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PricingSchemeServiceImpl extends ServiceImpl<PricingSchemeMapper, PricingScheme> implements IPricingSchemeService {
    @Autowired
    private IPricingSchemeDetailService pricingSchemeDetailService;

    @Autowired
    private IPriceFieldService priceFieldService;

    @Override
    @Transactional
    public void savenew(PricingSchemeDTO pricingSchemeDTO) {
        //获取所有价格字段
        List<Long> detailFields = pricingSchemeDTO.getDetailList().stream().map(PricingSchemeDTO.PricingSchemeDetailDTO::getPriceFieldId).toList();
        int fieldCount = detailFields.size();
        if (fieldCount==0)throw new PriceSchemeException("请检查价格字段是否正确！");
        long count = priceFieldService.count(new LambdaQueryWrapper<>(PriceField.class).in(PriceField::getId, detailFields));
        //检查所有价格字段是否存在
        if (count!= fieldCount)throw new PriceSchemeException("请检查价格字段是否正确！");
        PricingScheme pricingScheme = BeanUtil.copyProperties(pricingSchemeDTO, PricingScheme.class);
        pricingScheme.setStatus(1);
        pricingScheme.setVersion(1);
        save(pricingScheme);
        List<PricingSchemeDTO.PricingSchemeDetailDTO> detailList = pricingSchemeDTO.getDetailList();

        List<PricingSchemeDetail> processedDetailList = detailList.stream()
                .map(i -> {
                    PricingSchemeDetail copied = BeanUtil.copyProperties(i, PricingSchemeDetail.class);
                    copied.setSchemeId(pricingScheme.getId());
                    return copied;
                })
                //.peek(i -> i.setSchemeId(pricingScheme.getId()))//对每个元素进行修改的操作
                .toList();
        pricingSchemeDetailService.saveBatch(processedDetailList);
    }

    @Override
    @Transactional
    public void updateByDTO(PricingSchemeDTO pricingSchemeDTO) {
        // 1. 获取现有价格方案
        PricingScheme pricingScheme = getById(pricingSchemeDTO.getId());
        if (pricingScheme == null) {
            throw new CanNotFoundException(MessageConstant.CAN_NOT_FOUND);
        }

        // 2. 更新价格方案的基本信息
        BeanUtil.copyProperties(pricingSchemeDTO, pricingScheme, "id", "detailList");

        // 3. 更新价格方案详情
        // 删除现有详情
        pricingSchemeDetailService.remove(new LambdaQueryWrapper<>(PricingSchemeDetail.class)
                .eq(PricingSchemeDetail::getSchemeId, pricingScheme.getId()));

        // 添加新的详情
        List<PricingSchemeDetail> newDetails = pricingSchemeDTO.getDetailList().stream()
                .map(detailDTO -> {
                    PricingSchemeDetail detail = new PricingSchemeDetail();
                    BeanUtils.copyProperties(detailDTO, detail);
                    detail.setSchemeId(pricingScheme.getId());
                    return detail;
                })
                .toList();

        pricingSchemeDetailService.saveBatch(newDetails);

        // 4. 保存更新后的价格方案
        updateById(pricingScheme);
  }

    @Override
    public List<PricingSchemeVO> listAll() {
        MPJLambdaWrapper<PricingScheme> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .selectAsClass(PricingScheme.class, PricingSchemeVO.class)
                .selectCollection(PricingSchemeDetail.class, PricingSchemeVO::getDetailList)
                .leftJoin(PricingSchemeDetail.class, PricingSchemeDetail::getSchemeId, PricingScheme::getId);
        return baseMapper.selectJoinList(PricingSchemeVO.class, wrapper);
    }
    @Override
    public PricingSchemeVO getVoById(Long id){
        MPJLambdaWrapper<PricingScheme> wrapper = new MPJLambdaWrapper<>();
        wrapper
                .selectAsClass(PricingScheme.class, PricingSchemeVO.class)
                .selectCollection(PricingSchemeDetail.class, PricingSchemeVO::getDetailList)
                .leftJoin(PricingSchemeDetail.class, PricingSchemeDetail::getSchemeId, PricingScheme::getId)
                .eq(PricingScheme::getId, id);
        return baseMapper.selectJoinOne(PricingSchemeVO.class, wrapper);
    }
}